package com.sf.fy.uav.user.rest;

import com.sf.fy.uav.common.awt.JwtUtil;
import com.sf.fy.uav.common.request.user.ReceivingInformationAddRequest;
import com.sf.fy.uav.common.request.user.ReceivingInformationRequest;
import com.sf.fy.uav.common.response.user.ReceivingInformationResponse;
import com.sf.fy.uav.user.entity.ReceivingInformation;
import com.sf.fy.uav.user.service.IReceivingInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 收货信息表 前端控制器
 * </p>
 *
 * @author LTQ
 * @since 2023-04-04
 */
@Controller
@RequestMapping("/sys/receivingInformation/")
public class ReceivingInformationRest {

    @Autowired
    private IReceivingInformationService iReceivingInformationService;

    //    @PostMapping("add")
    public void add(@RequestBody ReceivingInformationRequest receivingInformationRequest) {
        String userId = JwtUtil.getUserId();
        receivingInformationRequest.setUserId(userId);
        iReceivingInformationService.add(receivingInformationRequest);
    }

    public void addAddress(@RequestBody ReceivingInformationAddRequest receivingInformationAddRequest) {
        String userId = JwtUtil.getUserId();
        receivingInformationAddRequest.setUserId(userId);
        iReceivingInformationService.addAddress(receivingInformationAddRequest);
    }

    public void editAddress(@RequestBody ReceivingInformationRequest receivingInformationRequest) {
        String userId = JwtUtil.getUserId();
        receivingInformationRequest.setUserId(userId);
        iReceivingInformationService.editAddress(receivingInformationRequest);
    }

    /**
     * 获取最近n条
     */
//    @GetMapping("get-last-ten")
    @Cacheable(value = "RESCUE::RECEIVING_INFORMATION", key = "#userId")
    public List<ReceivingInformationResponse> getLastTen(@RequestParam String userId, @RequestParam Integer num) {
        return iReceivingInformationService.getLastTen(num);
    }

    //    @Cacheable(value = "CITY::RECEIVING_INFORMATION", key = "#userId")
    public List<ReceivingInformationResponse> getLastTenAddress(@RequestParam String userId, @RequestParam Integer num, @RequestParam Integer page, @RequestParam Integer size) {
        return iReceivingInformationService.getLastTenAddress(num, page, size);
    }

    @GetMapping("get-total")
    public Long getTotal() {
        return iReceivingInformationService.getTotal();
    }

    //    @PostMapping("delete")
    public void delete(@RequestParam String id) {
        String userId = JwtUtil.getUserId();
        iReceivingInformationService.delete(id, userId);
    }

    public void deleteRescue(@RequestParam String id) {
        String userId = JwtUtil.getUserId();
        iReceivingInformationService.deleteRescue(id, userId);
    }

    public ReceivingInformation getPhoneNum(String id) {
        return iReceivingInformationService.getPhoneNum(id);
    }

    public void editAddressRescue(ReceivingInformationRequest receivingInformationRequest) {
        String userId = JwtUtil.getUserId();
        receivingInformationRequest.setUserId(userId);
        iReceivingInformationService.edit(receivingInformationRequest);
    }
}
